import { CHANGE_FOOTER_BAR_SELECTED_INDEX, INIT_FOOTER_BAR_LIST } from "@/constants/footerBar";
import api from "@/service/apiRequest"

export const initFooterBar = (payload) => {
    return {
        type: INIT_FOOTER_BAR_LIST,
        payload
    }
}

export const updateSelectedIndex = (payload) => {
    return {
        type: CHANGE_FOOTER_BAR_SELECTED_INDEX,
        payload
    }
}

export function asyncRequestFooterBar(footerBarIndex) {
    return async (dispatch) => {
        // 异步请求
        let res = await api.get("/api/menus", { category: "SYS_FOOTER_BAR" });
        if (res) {
            let list = res.map(item => Object.assign({}, { 
                title: item.menuName,
                image: item.menuIcon,
                selectedImage: item.menuSelectedIcon,
                menuCode: item.menuCode,
                category: item.category,
                menuUrl: item.menuUrl,
            }))
            dispatch(initFooterBar(list));
            dispatch(updateSelectedIndex(footerBarIndex));
        }
    }
} 